package com.ws.test;

import com.ws.constant.CompanyConstant;
import com.ws.dao.BikeDao;
import com.ws.dao.impl.BikeDaoImpl;
import com.ws.entity.*;

import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author 王硕
 * @version jdk21
 * @className BikeMgr
 * @description
 * @date 2024/11/16 14:26
 */
public class BikeMgr {
    private static final Scanner scanner = new Scanner(System.in);
    private static BikeDao bikeDao = new BikeDaoImpl();
    private static  BikeCompany[] companiesArray = new BikeCompany[3];
    private static BaseBike[] HLBike = new HLBike[3];
    private static BaseBike[] MTBike = new MTBike[3];
    private static BaseBike[] QJBike = new QJBike[3];
    public void menu() {
        companiesArray[0] = new BikeCompany().setCompanyId(1).setCompanyName(CompanyConstant.HELLO_COMPANY_NAME).setBikes(new HLBike[3]).setSum(3).setCount(100);
        companiesArray[1] = new BikeCompany().setCompanyId(2).setCompanyName(CompanyConstant.MEITUAN_COMPANY_NAME).setBikes(new MTBike[3]).setSum(3).setCount(100);
        companiesArray[2] = new BikeCompany().setCompanyId(3).setCompanyName(CompanyConstant.QINGJU_COMPANY_NAME).setBikes(new QJBike[3]).setSum(3).setCount(100);

        for (int index = 0; index < 3; index++) {
            companiesArray[0].getBikes()[index] = new HLBike().setBid(1000 + index).setBname(companiesArray[0].getCompanyName() + "单车" + (100 + index)).setStatus(1);
            companiesArray[1].getBikes()[index] = new MTBike().setBid(2000 + index).setBname(companiesArray[1].getCompanyName() + "单车" + (200 + index)).setStatus(1);
            companiesArray[2].getBikes()[index] = new QJBike().setBid(3000 + index).setBname(companiesArray[2].getCompanyName() + "单车" + (300 + index)).setStatus(1);
        }
        while (true) {
            System.out.println("——————————————————————————————");
            System.out.println("\uF0751:投放Bike\n" +
                    "\uF0752:查看Bike\n" +
                    "\uF0753:删除Bike\n" +
                    "\uF0754:借出Bike\n" +
                    "\uF0755:归还Bike\n" +
                    "\uF0756:Bike借出排行榜\n" +
                    "\uF0757:退出");
            System.out.println("——————————————————————————————");
            System.out.print("请选择:");
            try {
                int i = scanner.nextInt();
                switch (i) {
                    case 1:
                        putbike();
                        break;
                    case 2:
                        bikeDao.viewBikeCompany(companiesArray);
                        break;
                    case 3:
                        deleteBike();
                        break;
                    case 4:
                        loanBike();
                        break;
                    case 5:
                        returnBike();
                        break;
                    case 6:
                        bikeDao.viewLoanRank(companiesArray);
                        break;
                    case 7:
                        System.out.println("————>欢迎下次使用<————");
                        scanner.close();
                        System.exit(0);
                        break;
                    default:
                        System.out.println("你输入的数据错误");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
            returnMain();
        }
    }

    private void putbike() {
        for (BikeCompany bikeCompany : companiesArray) {
            System.out.println(bikeCompany.getCompanyId() + ":" + bikeCompany.getCompanyName());
        }
        System.out.println("请选择要投放的单车品牌:");
        int id = scanner.nextInt();
        BikeCompany company = getCompany(id);
        if (company == null) {
            System.out.println("输入的公司ID无效，无法投放单车。");
            return;
        }
        System.out.println("请录入要投放的数量:");
        int num = scanner.nextInt();
        bikeDao.dropBike(company, num);
    }

    private BikeCompany getCompany(int id) {
        for (BikeCompany bikeCompany : companiesArray) {
            if (Objects.equals(bikeCompany.getCompanyId(),id)){
                return bikeCompany;
            }
        }
        return null;
    }

    public static void returnMain() {
        boolean shouldReturn = false;
        while (!shouldReturn) {
            System.out.println("输入0返回上一级:");
            try {
                int i = scanner.nextInt();
                if (i == 0) {
                    shouldReturn = true;
                } else {
                    System.out.println("你的输入有误，请输入0返回上一级");
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入数字");
                scanner.next();
            }
        }
    }
    private void deleteBike() {
        System.out.println("请选择要删除单车的公司序号：");
        for (int i = 0; i < companiesArray.length; i++) {
            System.out.println((i + 1) + "." + companiesArray[i].getCompanyName());
        }
        int companyIndex = scanner.nextInt() - 1;

        if (companyIndex < 0 || companyIndex >= companiesArray.length || companiesArray[companyIndex] == null) {
            System.out.println("输入的公司序号无效，无法删除单车。");
            return;
        }
        BikeCompany company = companiesArray[companyIndex];
        System.out.println("公司名称: " + company.getCompanyName() + " 的单车信息如下：");
        bikeDao.viewBike(company.getBikes());
        System.out.print("请输入要删除的单车编号：");
        int bid = scanner.nextInt();
        bikeDao.deleteBike(company, bid);
    }
    private void loanBike() {
        System.out.println("请选择要借出单车的公司序号：");
        for (int i = 0; i < companiesArray.length; i++) {
            System.out.println((i + 1) + "." + companiesArray[i].getCompanyName());
        }
        int companyIndex = scanner.nextInt() - 1;

        if (companyIndex < 0 || companyIndex >= companiesArray.length || companiesArray[companyIndex] == null) {
            System.out.println("输入的公司序号无效，无法借出单车。");
            return;
        }
        BikeCompany company = companiesArray[companyIndex];
        System.out.println("公司名称: " + company.getCompanyName() + " 的单车信息如下：");
        bikeDao.viewBike(company.getBikes());
        System.out.print("请输入要借出的单车编号：");
        int bid = scanner.nextInt();
        bikeDao.loanBike(company, bid);
    }
    private void returnBike() {
        System.out.println("请选择要归还单车的公司序号：");
        for (int i = 0; i < companiesArray.length; i++) {
            System.out.println((i + 1) + "." + companiesArray[i].getCompanyName());
        }
        int companyIndex = scanner.nextInt() - 1;

        if (companyIndex < 0 || companyIndex >= companiesArray.length || companiesArray[companyIndex] == null) {
            System.out.println("输入的公司序号无效，无法归还单车");
            return;
        }
        BikeCompany company = companiesArray[companyIndex];
        System.out.println("公司名称: " + company.getCompanyName() + " 的单车信息如下：");
        bikeDao.viewBike(company.getBikes());
        System.out.print("请输入要归还的单车编号：");
        int bid = scanner.nextInt();
        bikeDao.returnBike(company, bid);
    }
}
